//
// Created by konglinglong on 2020/9/21.
//

#include <stdio.h>
#include <string>
#include "../../../lib/doctest.h"
#include "../../../../lib/3rdParty/toml/toml.h"

using namespace std;

TEST_SUITE ("TOML_TS")
{
    TEST_CASE ("TOML_TC")
    {
        int32_t rc;
        uint32_t loop;
        char buffer[4096];
        toml_table_t *pTomlTableRoot;
        SUBCASE("TOML_SC_PARSE")
        {
            const char* filePath = O5G_SOURCE_DIR "/tests/moduleTests/lib/toml/test.toml";
            FILE* fp = fopen(filePath, "rb");
            REQUIRE(fp);
            pTomlTableRoot = toml_parse_file(fp, buffer, sizeof(buffer));
            REQUIRE(pTomlTableRoot);

            toml_raw_t raw;
            char* strData;
            raw = toml_raw_in(pTomlTableRoot, "Active_eNBs");
            REQUIRE(raw);
            rc = toml_rtos(raw, &strData);
            REQUIRE(0 == rc);
            REQUIRE(string(strData) == "eNB-CU-Eurecom-LTEBox");

            toml_table_t* pNbTable;
            pNbTable = toml_table_in(pTomlTableRoot, "eNBs");
            REQUIRE(pNbTable);
            raw = toml_raw_in(pNbTable, "eNB_ID");
            REQUIRE(raw);
            int64_t intData;
            rc = toml_rtoi(raw, &intData);
            REQUIRE(0 == rc);
            REQUIRE(intData == 0xe00);

            toml_table_t* pPlmnTable;
            pPlmnTable = toml_table_in(pNbTable, "plmn_list");
            REQUIRE(pPlmnTable);
            raw = toml_raw_in(pPlmnTable, "mcc");
            REQUIRE(raw);
            rc = toml_rtoi(raw, &intData);
            REQUIRE(0 == rc);
            REQUIRE(intData == 208);

            toml_array_t *pArray;
            pArray = toml_array_in(pNbTable, "component_carriers");
            REQUIRE(pArray);
            rc = toml_array_nelem(pArray);
            REQUIRE(2 == rc);

            toml_table_t* pCarrierTable;
            pCarrierTable = toml_table_at(pArray, 0);
            REQUIRE(pCarrierTable);
            raw = toml_raw_in(pCarrierTable, "node_function");
            REQUIRE(raw);
            rc = toml_rtos(raw, &strData);
            REQUIRE(0 == rc);
            REQUIRE(string(strData) == "3GPP_eNodeB0");

            pCarrierTable = toml_table_at(pArray, 1);
            REQUIRE(pCarrierTable);
            raw = toml_raw_in(pCarrierTable, "node_function");
            REQUIRE(raw);
            rc = toml_rtos(raw, &strData);
            REQUIRE(0 == rc);
            REQUIRE(string(strData) == "3GPP_eNodeB1");

            toml_free(pTomlTableRoot);
            fclose(fp);
        }
    }
}

